// let count = 10;
const state = {
  videoRecord: null,
  currentTime: {},
};
const getters = {
  videoRecord(state) {
    return (
      state.videoRecord ||
      JSON.parse(window.sessionStorage.getItem("videoRecord"))
    );
  },
};
const actions = {
  // changeRecord({commit},{id,currentTime}){
  // }
};
const mutations = {
  CHANGERECORD(state, { id, currentTime }) {
    if (!state.videoRecord) {
      const arr = window.sessionStorage.getItem("videoRecord");
      state.videoRecord = arr ? arr : [];
    }
    // if (!state.videoRecord[0]) {
    //   state.videoRecord = JSON.parse(
    //     window.sessionStorage.getItem("videoRecord")
    //   );
    // }
    const { videoRecord } = state;
    // console.log("state", state, id, currentTime);
    const obj = videoRecord.find((item) => item.mvId === id);
    // 如果存在
    if (obj) {
      const index = videoRecord.indexOf(obj);
      if (index === 0) {
        state.videoRecord.splice(0, 1, { mvId: id, [id]: currentTime });
        window.sessionStorage.setItem(
          "videoRecord",
          JSON.stringify(state.videoRecord)
        );
        return;
      }
      const item = state.videoRecord.splice(index, 1);
      state.videoRecord.unshift(item[0]);
      window.sessionStorage.setItem(
        "videoRecord",
        JSON.stringify(state.videoRecord)
      );
      return;
    }
    // 如果不存在
    if (videoRecord.length >= 10) {
      state.videoRecord.splice(9, 1);
    }
    state.videoRecord.unshift({ mvId: id, [id]: currentTime });
    window.sessionStorage.setItem(
      "videoRecord",
      JSON.stringify(state.videoRecord)
    );
  },
};
export default {
  state,
  getters,
  actions,
  mutations,
};
